﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/
using System;
using System.Web.UI;

namespace Mobile
{
    public static class ControlHelper
    {
        #region Methods
        public static Control GetControlById(Control parentControl, string id)
        {
            Control control = null;
            if ((parentControl != null) && (parentControl.Controls.Count > 0))
            {
                foreach (Control childControl in parentControl.Controls)
                {
                    if (childControl != null)
                    {
                        if (childControl.ID == id)
                        {
                            control = childControl;
                            break;
                        }
                        else
                        {
                            control = GetControlById(childControl, id);
                            if (control != null)
                            {
                                break;
                            }
                        }
                    }
                    
                }
            }
            return control;
        }

        public static string MakeUniqueId()
        {
            Guid guid = Guid.NewGuid();
            return guid.ToString();
        }

        public static string AddUniqueInUrl(string url)
        {
            if ((!String.IsNullOrEmpty(url)) && (url.IndexOf("#") == -1))
            {
                if (url.IndexOf("?") == -1)
                {
                    url += String.Format("?{0}={1}", QueryString.UniqueIdKey, MakeUniqueId());
                }
                else //if url has querystring
                {
                    url += String.Format("&{0}={1}", QueryString.UniqueIdKey, MakeUniqueId());
                }
            }
            return url;
        }
        #endregion
    }
}
